<?php
/**
 * Controller_Default
 * Класс Controller_Default является контроллером по-умолчанию
 * 
 * @author Sayp
 */
class Controller_Default extends Controller_Base {
	
	public function action() {
		if (!City::getSub()) throw new Exception_SiteError('', 404);
		$url = Service_Escape::absolutelyEscapeString($this->arrRequest[0]);
		
		// Смотрим, не страна ли это
		$objCountry = new Country();
		$objCountry->find('first', array(
			'conditions' => "`country_url` = '" . $url . "'"
		));
		
		if (!$objCountry->id) throw new Exception_SiteError('', 404);
		
		$objArticle = new Article();
		$objCity = City::getCity();
		$url = Service_Escape::absolutelyEscapeString($this->arrRequest[1]);
// 		switch ($url) {
// 			case '': {
				$objArticle->find('first', array(
					'conditions' => '`is_main` = 1 AND `ID_city` = ' . $objCity->id . ' AND `ID_essence_type` = ' . Country::getEssenceType() . ' AND `ID_essence` = ' . $objCountry->id
				));
				
				if ($objArticle->id) {
					if (!$url) $this->actionCountry($objCity, $objCountry, $objArticle);
					else {
						$objResort = new Resort();
						$objResort->find('first', array(
							'conditions' => "`ID_country` = '" . $objCountry->id . "' AND `resort_url` = '" . $url . "'"
						));
						
						if (!$objResort->id) throw new Exception_SiteError('', 404);
						$this->actionResort($objCity, $objCountry, $objResort);
					}
				}
				else throw new Exception_SiteError('', 404);
					
			//	break;
// 			}
// 			default: throw new Exception_SiteError('', 404);
// 		}
		
	}
	
	/**
	 * Страница страны
	 * @param City $objCity - объект города
	 * @param Country $objCountry - объект страны
	 * @param Article $objArticle - объект, связанный со статьей
	 */
	private function actionCountry(City $objCity, Country $objCountry, Article $objArticle) {
		$arrPhotoIds = $objCountry->getPhotosIds();
		
		$arrWeathers = $objCountry->getWeather();
		$arrWeathersResort = $objCountry->getWeatherCountryResort();
		
		$arrResorts = $objCountry->getResorts();
		$arrResorts = Service_ArrayConverter::makeKeyFromColumn($arrResorts, 'ID_resort');
		
		$minPrice = $objCountry->getField('price_min') ? $objCountry->getField('price_min') : 15000; 
		
		$this->title = $objArticle->getField('article_title') ? $objArticle->getField('article_title') : Service_SeoTemplate::getTemplate('тайтл страны', $objCity->getField('city_name_2'), $objCountry->getField('country_name_6'), $objCountry->getField('country_name_2'));
		$this->pageHead = $objArticle->getField('article_pagehead') ? $objArticle->getField('article_pagehead') : Service_SeoTemplate::getTemplate('заголовок страны', $objCity->getField('city_name_2'), $objCountry->getField('country_name_6'));
		$this->description = $objArticle->getField('article_description') ? $objArticle->getField('article_description') : Service_SeoTemplate::getTemplate('дескрипшен страны', $objCountry->getField('country_name_6'), $objCity->getField('city_name_2'), $minPrice);
		
		$this->showHeader();
			View_Tour::showCountryPage($objCountry, $arrPhotoIds, $objArticle, $arrResorts, $arrWeathers, $arrWeathersResort);
		$this->showFooter();
	}
	
	/**
	 * Страница курорта
	 * @param City $objCity - объект города
	 * @param Country $objCountry - объект страны
	 * @param Resort $objResort - объект курорта
	 */
	private function actionResort(City $objCity, Country $objCountry, Resort $objResort) {
		$arrWeather = $objResort->getWeather();
		$arrOtherResort = $objResort->getOtherResorts();
		$this->title = $this->pageHead = $objResort->getField('resort_name');
		
		$this->showHeader();
			echo '<div class="article">';
				echo '<img src="' . View_Photo::getUrl($objResort->getField('ID_photo'), '400x250') . '" class="poster">';
				echo Service_SeoTemplate::getResortSeoText($objCity, $objCountry, $objResort, $arrOtherResort, $arrWeather);
			echo '</div>';
			if (!empty($arrWeather)) View_Tour::showWeatherResort($arrWeather);
		$this->showFooter();
	}
	
}